/*
 * Copyright (C) 1997-2020 康成投资（中国）有限公司
 *
 * http://www.rt-mart.com
 *
 * 版权归本公司所有，不得私自使用、拷贝、修改、删除，否则视为侵权
 */
/*
 * com.zw.admin.server.service.impl
 * @author xiaoqun.liu
 * @date 2021年5月12日 下午8:16:56
 */
package com.zw.admin.server.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zw.admin.server.component.StockComponent;
import com.zw.admin.server.dao.PGoodsDao;
import com.zw.admin.server.dao.PSaleOrderItemsDao;
import com.zw.admin.server.dao.PSaleOrdersDao;
import com.zw.admin.server.dto.ResponseInfo;
import com.zw.admin.server.model.PGoods;
import com.zw.admin.server.model.PSaleOrderItems;
import com.zw.admin.server.model.PSaleOrders;
import com.zw.admin.server.service.SaleOrderSerivce;
import com.zw.admin.server.utils.DateUtils2;

/**
 *
 * @author xiaoqun.liu
 * @date 2021年5月12日 下午8:16:56
 */
@Service
public class SaleOrderSerivceImpl implements SaleOrderSerivce {
	@Autowired
	private PGoodsDao pGoodsDao;

    @Autowired
    private PSaleOrdersDao pSaleOrdersDao;
    

    @Autowired
    private PSaleOrderItemsDao pSaleOrderItemsDao;
    
    @Autowired
    private StockComponent stockComponent;
    
	public ResponseInfo saveAll(JSONObject request) {
		 
		String qtysStr =request.getString("qtys");
		List<JSONObject> listQty=JSONObject.parseArray(qtysStr,JSONObject.class);
		JSONArray skuArr =  request.getJSONArray("sku");
		JSONArray quantityArr =  request.getJSONArray("quantity");
		
		BigDecimal quantity_total =request.getBigDecimal("quantity_total");
		//支付金额
		BigDecimal payAmount =request.getBigDecimal("payAmount");
		
		if(skuArr==null || quantityArr==null) {
			return new ResponseInfo("-1","未提交商品");
		}
		Map<String,BigDecimal > mapSkuQty= new HashMap<>();
		for(int i=0;i<skuArr.size();i++) {
			String sku=skuArr.getString(i);
			BigDecimal quantity=quantityArr.getBigDecimal(i);
			if(StringUtils.isNotBlank(sku) && BigDecimal.ZERO.compareTo(quantity)<0 ) {
				mapSkuQty.put(sku, quantity);
			}
		}
		if(mapSkuQty ==null ||  mapSkuQty.isEmpty()) {
			return new ResponseInfo("-1","未提交有效商品或数量");
		}
		List<PGoods> listPGoods = pGoodsDao.listBySku(mapSkuQty.keySet().stream().collect(Collectors.toList()));
		PSaleOrders pSaleOrders = new PSaleOrders();
		 Integer buyDate =  Integer.parseInt( DateUtils2.getNow(DateUtils2.FORMAT_SHORT2)) ; 
		pSaleOrders.setBuydate(buyDate);
		pSaleOrders.setPayAmount(payAmount);
		pSaleOrders.setQuantity(quantity_total);
		
		int rSOrder =  pSaleOrdersDao.insert(pSaleOrders);
		String orderNo =  buyDate.toString() + String.format("%05d", pSaleOrders.getId()); 
		
		BigDecimal total_qty= new BigDecimal(0);
		BigDecimal totalAmout = new BigDecimal(0);
		for(PGoods pGoods :listPGoods ) {
			PSaleOrderItems pSaleOrderItems= new PSaleOrderItems();
			pSaleOrderItems.setBuydate( buyDate );
			pSaleOrderItems.setItemNo(pGoods.getItemNo());
			pSaleOrderItems.setSku(pGoods.getSku());
			pSaleOrderItems.setSalePrice(pGoods.getPrice());
			pSaleOrderItems.setPrice(pGoods.getPrice());
			pSaleOrderItems.setOrderNo(orderNo);
			
			
			BigDecimal qty = mapSkuQty.get(pGoods.getSku());
			total_qty=total_qty.add(qty);
			totalAmout=totalAmout.add(qty.multiply( pGoods.getPrice() ));
			
			pSaleOrderItems.setQuantity(qty);
			
			pSaleOrderItemsDao.insert(pSaleOrderItems);
			stockComponent.saleStock(pSaleOrderItems);
		}
		PSaleOrders upPSaleOrders = new PSaleOrders();
		upPSaleOrders.setId(pSaleOrders.getId());
		upPSaleOrders.setOrderNo(orderNo);
		upPSaleOrders.setAmount(totalAmout);
		upPSaleOrders.setQuantity(total_qty);
		upPSaleOrders.setSaleAmount(totalAmout);
		upPSaleOrders.setStatus(1);
		pSaleOrdersDao.update(upPSaleOrders);
		
		return new ResponseInfo("0","提交保存成功 ");
	}
	
	public ResponseInfo addItem(JSONObject request) {
		 
		String sku =request.getString("sku");
		if(StringUtils.isBlank(sku)) {
			return new ResponseInfo("-1","Sku必填");
		}
		PGoods pGoods=pGoodsDao.getBySku(sku);
		if(pGoods==null ) {
			return new ResponseInfo("-2","Sku无效");
		}
		return new ResponseInfo("0","找到商品",pGoods);
	}
}
